﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using StyleMVVM.DependencyInjection;
using StyleMVVM.DependencyInjection.Impl;
#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

#else
using System.Windows;
using System.Windows.Controls;

#endif

namespace StyleMVVM.BootstrapperHelpers
{
	public sealed class PageActivationStrategyFactor : IExportStrategyFactory
	{
		public IExportStrategy CreateStrategy(IDependencyInjectionContainer container,
		                                      Type type,
		                                      IEnumerable<object> attributes,
		                                      bool alreadyExported)
		{
			IExportStrategy returnValue = null;

			if (!alreadyExported)
			{
				Type pageType = type.GetTypeInfo().BaseType;

				while (pageType != null && pageType != typeof(Page) && pageType != typeof(UserControl))
				{
					pageType = pageType.GetTypeInfo().BaseType;
				}

				if (pageType != null)
				{
					returnValue = createCompiledStrategy(type, new[] { type.Name });

					if (returnValue != null)
					{
						returnValue.Environment = container.Environment;
						returnValue.Priority = 0;
					}
				}
			}

			return returnValue;
		}

		private IExportStrategy createCompiledStrategy(Type closetype, IEnumerable<string> exportNames)
		{
			return
				new AttributedExportStrategy(exportNames, closetype);
		}
	}
}